/*
 * @Title: 添加新收货地址
 * @Author: zhj
 * @Date:   2018-10-13
 * @Last Modified by:   zhj
 * @Last Modified time: 2018-10-18
 */
var dataInfo = new Object(),
	areaList,
	template = {
		init: function() {
			dataInfo = new Vue({
				el: '#app',
				data: {
					show: false,
					type: false, //是否设为默认
					areaList: addressInfo,
					addressId: "",
					addAdress: { //提交的地址信息
						userId: userId,
						name: '',
						phone: '',
						province: '',
						city: '',
						district: '',
						address: '',
						defaultAddress: 1
					},
				},
				methods: {
					choseOver: function(data) { //用户选择地址结束
						this.addAdress.province = data[0].name;
						this.addAdress.city = ' ' + data[1].name + ' ';
						this.addAdress.district = data[2].name;
						this.show = false;
					},
					saveAddress: function() { //新增默认地址
						if(!this.addAdress.name || !this.addAdress.name.trim()) {
							this.$toast('请输入收货人姓名');
						} else if(!this.addAdress.phone) {
							this.$toast('请输入收货人手机号');
						} else if(!(/^1[0-9]\d{9}$/.test(this.addAdress.phone))) {
							this.$toast('请输入正确的手机号');
						} else if(!this.addAdress.district) {
							this.$toast('请选择收货区域');
						} else if(!this.addAdress.address || !this.addAdress.address.trim()) {
							this.$toast('请输入详细地址');
						} else {
							this.$toast.loading({
								forbidClick: true,
								message: '收货信息提交中...',
								loadingType: 'spinner',
								duration: 0,
							});
							if(dataInfo.type){
								dataInfo.addAdress.defaultAddress = 0
							}
							ajaxTemplate(addressTest + SERVICE.addAddressOrder, function(data) {
								if(data.tag == 0) {
									location.href = '../html/addressList.html?from=' + getQueryString('from')+'&buyWay='+getQueryString('buyWay');
								} else {
									dataInfo.init.$toast.clear();
									dataInfo.init.$toast(data.errMsg);
								};
							}, JSON.parse(JSON.stringify(this.addAdress)));
						};
					}
				}
			});
		}
	};
template.init();